package com.zhu.service.cms.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.commons.utils.R;
import com.zhu.service.cms.pojo.CmsAd;
import com.zhu.service.cms.service.ICmsAdService;

import com.zhu.service.cms.vo.CmsAdVo;
import com.zhu.service.edu.pojo.vo.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告推荐 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-08-14
 */
@RestController
@RequestMapping("admin/modules")
@Api(tags = "具体广告模块")
public class CmsAdController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ICmsAdService  adService;
 
    @PostMapping("save")
    @ApiOperation(value = "新增推荐分类")
    public R save(@RequestBody CmsAd CmsAd){
        boolean save = adService.save(CmsAd);
        if(save){
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询广告分类")
    public R getadTypeById(@PathVariable String id){
        CmsAd CmsAd = adService.getById(id);
        if(CmsAd!=null){
            return R.ok().data("item",CmsAd);
        }
        return R.error().message("error！");
    }

    @PutMapping("update")
    @ApiOperation(value = "更新推荐分类")
    public R updateadType(@RequestBody CmsAd CmsAd){
        boolean b = adService.updateById(CmsAd);
        if(b){
            return R.ok().message("更新成功！");
        }
        return R.error().message("更新失败！");
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除推荐分类")
    public R removeById(@PathVariable String id)  {
        Boolean image = adService.removeImageById(id);
        boolean b = adService.removeById(id);
        if(b){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }

    @ApiOperation("推荐类别分页列表")
    @GetMapping("list/{currentPage}/{size}")
    public R listPage(@PathVariable Long currentPage,@PathVariable Long size,
                      CmsAdVo cmsAdVo){

        PageBean pageBean = adService.selectPages(currentPage,size,cmsAdVo);
        if(pageBean!=null){
            return R.ok().data("items",pageBean);
        }
        return  R.error().message("error！");


    }

    @ApiOperation("查询所有推荐类别")
    @GetMapping("list")
    public R list(){
        List<CmsAd> list = adService.list();
        if(list!=null){
            return R.ok().data("items",list);
        }
        return  R.error().message("error！");


    }

    @ApiOperation(value = "redis测试")
    @PostMapping("save-test")
      public R saveAd(@RequestBody CmsAd cmsAd){
     redisTemplate.opsForValue().set("ad",cmsAd);
     return R.ok();
}


}
